<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>入库信息总览</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport"
              content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
        <link rel="stylesheet" th:href="${urls.getForLookupPath('/css/font.css')}">
        <link rel="stylesheet" th:href="${urls.getForLookupPath('/css/xadmin.css')}">
        <script th:src="${urls.getForLookupPath('/lib/layui/layui.js')}" charset="utf-8"></script>
        <script type="text/javascript" th:src="${urls.getForLookupPath('/js/xadmin.js')}"></script>
    </head>
    <body class="instocklist">
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
               onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div shiro:hasPermission="S" class="layui-card-body ">
                            <div class="layui-form layui-inline">
                                <select name="ids">
                                    <option value="">请选择查询依据</option>
                                    <option value="repositoryid">仓库编号</option>
                                    <option value="productid">产品编号</option>
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input id="id" type="text" name="id" placeholder="请输入查询编号"
                                       autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" onclick="instockIdSearch()">
                                    <i class="layui-icon">&#xe615;</i></button>
                            </div>

                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input" id="searchtime" name="searchtime" type="text"
                                       placeholder="请选择查询时间" autocomplete="off">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" onclick="instockTimeSearch()">
                                    <i class="layui-icon">&#xe615;</i></button>
                            </div>
                        </div>
                        <div class="layui-card-header">
                            <button shiro:hasPermission="D" class="layui-btn layui-btn-danger" onclick="delAll()">
                                <i class="layui-icon">&#xe640;</i>批量删除
                            </button>
                            <button shiro:hasPermission="A" class="layui-btn" onclick="xadmin.open('添加入库记录','/usermanage/toStocking',540,480)">
                                <i class="layui-icon">&#xe61f;</i>添加入库记录
                            </button>
                            <button class="layui-btn layui-btn-normal" onclick="backtoInstockList()">
                                <i class="layui-icon">&#xe62d;</i>入库记录列表
                            </button>
                        </div>
                        <div class="layui-card-body layui-table-body layui-table-main">
                            <table class="layui-table layui-form">
                                <thead>
                                <tr>
                                    <th>
                                        <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">全选
                                    </th>
                                    <th>入库编号</th>
                                    <th>商品编号</th>
                                    <th>仓库编号</th>
                                    <th>入库数量</th>
                                    <th>入库时间</th>
                                    <th>经办人</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:each="instock:${page.list}">
                                    <td>
                                        <input type="checkbox" name="checkId" th:value="${instock.id}"
                                               lay-skin="primary">
                                    </td>
                                    <td th:text="${instock.id}"></td>
                                    <td th:text="${instock.pid}"></td>
                                    <td th:text="${instock.rid}"></td>
                                    <td th:text="${instock.amount}"></td>
                                    <td th:text="${#dates.format(instock.datetime, 'yyyy-MM-dd')}"></td>
                                    <td th:text="${instock.name}"></td>
                                    <td class="td-manage">
                                        <button shiro:hasPermission="C" class="layui-btn layui-btn-normal" th:value="${instock.id}"
                                                onclick="xadmin.open2('编辑','/usermanage/editInstock/',540,450, false, this)">
                                            <i class="layui-icon">&#xe642;</i>编辑
                                        </button>
                                        <button shiro:hasPermission="D" class="layui-btn layui-btn-danger" th:value="${instock.id}"
                                                onclick="member_del(this,'要删除的id')">
                                            <i class="layui-icon">&#xe640;</i>删除
                                        </button>
                                    </td>
                                </tr>

                                </tbody>
                            </table>
                        </div>
                        <div class="layui-card-body ">
                            <div class="page">
                                <!--当前页数小于等于1时，上一页还是显示第一页的信息-->
                                <div th:if="${page.pageNum} le 1">
                                    <a class="prev" th:href="@{/usermanage/instockList(pn=1)}">&lt;&lt;</a>
                                    <a class="num" th:href="@{/usermanage/instockList(pn=${page.pageNum})}">上一页</a>
                                    <span class="current" th:href="@{/usermanage/instockList(pn=${page.pageNum})}"
                                          th:text="${page.pageNum}"></span>
                                    <a class="num" th:if="${page.pageNum} lt ${page.pages}"
                                       th:href="@{/usermanage/instockList(pn=${page.pageNum+1})}">下一页</a>
                                    <a class="next" th:href="@{/usermanage/instockList(pn=${page.pages})}">&gt;&gt;</a>
                                </div>
                                <!--当前页数大于等于最后一页时，点击下一页还是显示最尾页页的信息-->
                                <div th:if="${page.pageNum} != 1 and ${page.pageNum} ge ${page.pages}">
                                    <a class="prev" th:href="@{/usermanage/instockList(pn=1)}">&lt;&lt;</a>
                                    <a class="num" th:href="@{/usermanage/instockList(pn=${page.pageNum-1})}">上一页</a>
                                    <span class="current" th:href="@{/usermanage/instockList(pn=${page.pageNum})}"
                                          th:text="${page.pageNum}"></span>
                                    <a class="num" th:href="@{/usermanage/instockList(pn=${page.pageNum})}">下一页</a>
                                    <a class="next" th:href="@{/usermanage/instockList(pn=${page.pages})}">&gt;&gt;</a>
                                </div>
                                <!--非首尾页-->
                                <div th:if="${page.pageNum} gt 1 and ${page.pageNum} lt ${page.pages}">
                                    <a class="prev" th:href="@{/usermanage/instockList(pn=1)}">&lt;&lt;</a>
                                    <a class="num" th:href="@{/usermanage/instockList(pn=${page.pageNum-1})}">上一页</a>
                                    <span class="current" th:href="@{/usermanage/instockList(pn=${page.pageNum})}"
                                          th:text="${page.pageNum}"></span>
                                    <a class="num" th:href="@{/usermanage/instockList(pn=${page.pageNum+1})}">下一页</a>
                                    <a class="next" th:href="@{/usermanage/instockList(pn=${page.pages})}">&gt;&gt;</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
        var option, time;
        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;

            laydate.render({
                elem: '#searchtime'
                , showBottom: false
                , trigger: 'click'
                , done: function (value) {
                    time = value;
                }
            });
            // 监听全选
            form.on('checkbox(checkall)', function (data) {

                if (data.elem.checked) {
                    $('tbody input').prop('checked', true);
                } else {
                    $('tbody input').prop('checked', false);
                }
                form.render('checkbox');
            });

            form.on('select', function (data) {
                option = data.value;
                console.log(option);
            });
        });

        /**
         * 单个删除
         * @param obj
         */
        function member_del(obj) {
            var id = obj.getAttribute("value");

            layer.confirm('确认要删除吗？', function () {

                $.ajax({
                    type: "GET",
                    url: "/usermanage/deleteOneStocking",
                    data: {'id': id},
                    success: function () {
                        layer.msg('删除成功', {icon: 1, time: 1000});
                        location.reload();
                    },
                    error: function () {
                        layer.msg('删除失败', {icon: 5, time: 1000});
                    }
                })
            });
        }

        /**
         * 批量删除
         * @param argument
         */
        function delAll(argument) {

            var checkNum = $("input[name='checkId']:checked").length;
            if (checkNum === 0) {
                layer.msg('至少选一项', {icon: 5});
                return;
            }

            layer.confirm('确认要删除吗？', function () {
                //捉到所有被选中的，发异步进行删除
                var checkList = new Array();
                $("input[name='checkId']:checked").each(function () {
                    checkList.push($(this).val())
                });

                $.ajax({
                    url: "/usermanage/deleteStockings",
                    type: "post",
                    data: {
                        checkList: checkList.toString()
                    },
                    datatype: "json",
                    success: function () {
                        location.reload();  // 删除成功，刷新页面
                        layer.msg('删除成功', {icon: 1, time: 1000});
                    },
                    error: function () {
                        layer.msg('删除失败', {icon: 5, time: 1000});
                    }
                })

            });
        }

        /**
         * 根据编号查询入库记录
         */
        function instockIdSearch() {
            var id = document.getElementById('id').value;
            if (option === 'repositoryid'){
                if (id.length === 0) {
                    layer.msg('请输入仓库编号！', {icon: 5, time: 1000});
                } else {
                    window.location.href = "/usermanage/resIdSearchStocking/" + id;
                }
            }else if(option === 'productid'){
                if (id.length === 0) {
                    layer.msg('请输入产品编号！', {icon: 5, time: 1000});
                } else {
                    window.location.href = "/usermanage/proIdSearchStocking/" + id;
                }
            }
        }

        /**
         * 根据日期查询入库记录
         */
        function instockTimeSearch() {
            //console.log(time);
            if (typeof(time) === "undefined"){  //判断日期是否为空
                layer.msg('请选择查询时间！', {icon: 5, time: 1000});
            } else {
                window.location.href = "/usermanage/timeSearchStocking/" + time;
            }
        }

        function backtoInstockList() {
            window.location.href = "/usermanage/instockList";
        }
    </script>
</html>